<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Session extends CI_Session {

    function __construct()
    {
        parent::__construct();
    }

	function country($contr)
	{	
		/*
		//Zeljko: //TODO:
 		//Remove following line to speed up the system, when ready with the country selection
		//Done.*/
		$this->unset_userdata('country_by_ip');
		
		if($this->userdata('country_by_ip')===FALSE)
		{
		
			try
			{
				$ip = ip2long($this->userdata('ip_address'));
				if($ip == 2130706433)
				{
					$ip = ip2long('85.23.204.39');
				}
				
				$query = $contr->db->query('select country from geo_country_ip ' .
								' where ip_low <= ' . $ip .  ' and ip_high >= ' . $ip . ';');
				$obj = $query->result();
				if((!is_array($obj)) || (count($obj)<1) || (strlen($obj[0]->country) == 0))
				{
					return FALSE;
				}
				$this->set_userdata('country_by_ip', $obj[0]->country);
			}
			catch (Exception $e) 
			{
				return FALSE;
			}
		}
		$this->unset_userdata('country_by_ip');
		return $this->userdata('country_by_ip');
	}

	function current_user_name()
	{
		return $this->userdata('username');
	}
	
	function current_user()
	{
		return $this->userdata('userinfo');
	}	
	
	function current_country()
	{
		return $this->userdata('country_by_ip');
	}
	
	function welcome_msg()
	{
		if($this->userdata('username')===FALSE) 
		{
			//echo 'Welcome, guest! <a href="' . site_url('login') . '">Log in/Register<a/>';
			return 'Welcome, guest! <a href="javascript:loginFromHere(\'' . uri_string(current_url()) . '\')">Log in/Register</a>';
		}
		else 
		{ 
			return 'Welcome, <a href="'.site_url("profile").'">' . $this->userdata('username')."</a> "
                . '<a href="' . site_url("login/dologout") . '">Logout</a>';
		}
	}		
}
